import telnetlib  
import time
  
# 服务器IP的前缀和要连接的服务器ID列表  
ip_prefix = '192.168.1.'  
server_ids = range(10, 19)  # 包括10到18  
  
# 假设每个服务器的用户名和密码都是相同的  
username = 'root'  
password = 'hiface123'  
  
# 要执行的命令列表  
commands = ['cd /date', 'rm -r *']  
  
for server_id in server_ids:  
    ip = ip_prefix + str(server_id)  
    print(f"Connecting to {ip}...")  
    try:  
        # 连接到服务器  
        tn = telnetlib.Telnet(ip, 23, timeout=10)  
  
        # 登录  
        
        tn.read_until(b"login: ")
        tn.write(b"root\n")
        time.sleep(0.005)
        tn.read_until(b"Password: ")
        tn.write(b"hiface123\n")
        print(f"Connecting to {ip} succeed!!!")  
  
        # 读取登录后的提示符，这可能会因服务器而异  
        # 这里假设提示符是 '$'  
        time.sleep(0.5)
  
        # 尝试执行命令
        tn.read_until(b"#")  
        tn.write(b"cd /date\n")
        time.sleep(0.005)
        tn.read_until(b"/date") 
        tn.write(b"rm -r *\n")
        time.sleep(0.005)
        tn.read_until(b"/date") 
        tn.write(b"ls\n")
        # data = tn.read_all()
        # if(data != "/date # "):
        #     print(f"Failed to rm")
  
        # 关闭连接  
        tn.write(b"exit\n")  
  
    except Exception as e:  
        print(f"Failed to connect to {ip}: {e}")  
  
    finally:  
        tn.close()  
  
print("Done.")